package com.outsider.util;

import org.eclipse.jetty.util.UrlEncoded;

import com.alibaba.fastjson.JSONObject;
import com.outsider.util.pageprocessor.AM_SearchPageprocessor;
import com.outsider.util.pageprocessor.DD_SearchPageprocessor;
import com.outsider.util.pageprocessor.JD_SearchPageprocessor;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

public class WatchDogeUtil {
	class Product {
		private String name;
		private String url;
		private String image;
		private String price;

		public String getUrl() {
			return url;
		}

		public void setUrl(String url) {
			this.url = url;
		}

		public String getImage() {
			return image;
		}

		public void setImage(String image) {
			this.image = image;
		}

		public String getPrice() {
			return price;
		}

		public void setPrice(String price) {
			this.price = price;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}
	}

	public static JSONObject search(String productName) {
		JSONObject result = new JSONObject();
		/*
		 * JSONObject jdResult=doJDsearch(productName); JSONObject
		 * amResult=doAMsearch(productName); JSONObject
		 * ddResult=doDDsearch(productName);
		 */

		final JSONObject jdResult = new JSONObject();
		final JSONObject amResult = new JSONObject();
		final JSONObject ddResult = new JSONObject();

		Runnable task1 = new Runnable() {
			public void run() {
				jdResult.putAll(doJDsearch(productName));
			}
		};
		Runnable task2 = new Runnable() {
			public void run() {
				amResult.putAll(doAMsearch(productName));
			}
		};
		Runnable task3 = new Runnable() {
			public void run() {
				ddResult.putAll(doDDsearch(productName));
			}
		};
		Thread thread1 = new Thread(task1);
		Thread thread2 = new Thread(task2);
		Thread thread3 = new Thread(task3);

		thread1.start();
		thread2.start();
		thread3.start();
		try {
			thread1.join();
			thread2.join();
			thread3.join();

		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		if (jdResult.isEmpty() == false)
			result.put("jdResult", jdResult);
		if (amResult.isEmpty() == false)
			result.put("amResult", amResult);
		if (ddResult.isEmpty() == false)
			result.put("ddResult", ddResult);
		return result;
	}

	private static String JD_SEARCH_URL = "http://search.jd.com/Search?enc=utf-8&keyword=";

	public static JSONObject doJDsearch(String target) {
		String targetUrl = JD_SEARCH_URL + UrlEncoded.encodeString(target);
		final JSONObject jsonObject = new JSONObject();
		class JD_ProductPipe implements Pipeline {
			public void process(ResultItems resultItems, Task task) {
				if (resultItems.get("name") == null)
					return;
				jsonObject.put("name", resultItems.get("name"));
				jsonObject.put("price", resultItems.get("price"));
				jsonObject.put("image", resultItems.get("image"));
				jsonObject.put("url", resultItems.get("url"));

			}
		}
		Spider.create(new JD_SearchPageprocessor()).addUrl(targetUrl).addPipeline(new JD_ProductPipe()).run();
		;
		return jsonObject;
	}

	private static String AM_SEARCH_URL = "https://www.amazon.cn/s/field-keywords=";

	public static JSONObject doAMsearch(String target) {
		String targetUrl = AM_SEARCH_URL + UrlEncoded.encodeString(target);
		final JSONObject jsonObject = new JSONObject();
		class AM_ProductPipe implements Pipeline {
			public void process(ResultItems resultItems, Task task) {
				if (resultItems.get("name") == null)
					return;
				jsonObject.put("name", resultItems.get("name"));
				jsonObject.put("price", resultItems.get("price"));
				jsonObject.put("image", resultItems.get("image"));
				jsonObject.put("url", resultItems.get("url"));

			}
		}
		Spider.create(new AM_SearchPageprocessor()).addUrl(targetUrl).addPipeline(new AM_ProductPipe()).run();
		return jsonObject;
	}

	private static String DD_SEARCH_URL = "http://search.dangdang.com/?key=";

	public static JSONObject doDDsearch(String target) {
		String targetUrl = DD_SEARCH_URL + target + "&show=big#J_tab";
		;
		final JSONObject jsonObject = new JSONObject();
		class DD_ProductPipe implements Pipeline {
			public void process(ResultItems resultItems, Task task) {
				if (resultItems.get("name") == null)
					return;
				jsonObject.put("name", resultItems.get("name"));
				jsonObject.put("price", resultItems.get("price"));
				jsonObject.put("image", resultItems.get("image"));
				jsonObject.put("url", resultItems.get("url"));

			}
		}
		Spider.create(new DD_SearchPageprocessor()).addUrl(targetUrl).addPipeline(new DD_ProductPipe()).run();
		return jsonObject;
	}

	public static void main(String args[]) {
		JSONObject jsonObject = WatchDogeUtil.search("Kindle");
		System.out.println(jsonObject.size());

	}
}
